package com.example.administrator.boxuegu.view;

import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;


import com.example.administrator.boxuegu.R;
import com.example.administrator.boxuegu.fragment.AdBannerFragment;

import java.util.ArrayList;
import java.util.List;

public class MyAdBanner {
    private View view;
    private LinearLayout ll_dots;
    private ViewPager  vp_adbanner;
    private int[] adsId;
    private List<ImageView> imageViewDots;
    private AppCompatActivity context;
    private int count;
    private int dotIndex;//表示当前蓝色小圆点序号
    private int oldDotIndex;//表示切换前蓝色小圆点序号
    private int index;//表示广告序号
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if(msg.what==1){
                vp_adbanner.setCurrentItem(index);
                autpPlay();
            }
        }
    };

    public MyAdBanner(AppCompatActivity context, int[] adsId) {//第二个参数是广告内容
        this.adsId=adsId;
        this.context=context;
        this.count=adsId.length;
        LayoutInflater layoutInflater=LayoutInflater.from(context);
        view=layoutInflater.inflate(R.layout.main_adbanner,null);
        initView();
    }

    private void initView(){
        vp_adbanner=view.findViewById(R.id.vp_adbanner);
        ll_dots=view.findViewById(R.id.ll_dots);
        ll_dots.setGravity(Gravity.CENTER);

        List<AdBannerFragment> fragments=new ArrayList<AdBannerFragment>();
        for(int i=0;i<count;i++){
            AdBannerFragment fragment=AdBannerFragment.newInstance(adsId[i]);
            fragments.add(fragment);
        }//下面为广告前面添加两张缓冲图片
        AdBannerFragment fragment=AdBannerFragment.newInstance(adsId[2]);
        fragments.add(0,fragment);
        fragment=AdBannerFragment.newInstance(adsId[0]);
        fragments.add(fragment);

        AdBannerAdapter adapter=new AdBannerAdapter(context.getSupportFragmentManager(),fragments);
        vp_adbanner.setAdapter(adapter);

        vp_adbanner.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            //监听侧滑事件
            @Override
            public void onPageScrolled(int i, float v, int i1) {
                //手在屏幕上滑动时执行，执行多次
            }
            @Override
            public void onPageSelected(int i) {
                //切换成功后执行
                if(i==0){//表示已经到达最左边，是图C
                    vp_adbanner.setCurrentItem(count,false);
                    index=count;
                    dotIndex=count-1;
                }else if(i==count+1){//表示已经到达最右边，是图A
                    vp_adbanner.setCurrentItem(1,false);
                    index=1;
                    dotIndex=0;
                }else {//表示在中间
                    index=i;
                    dotIndex=i-1;
                }
                setDot();
            }
            @Override
            public void onPageScrollStateChanged(int i) {
                //手离开屏幕执行
            }
        });

        initDots();
        index=1;
        vp_adbanner.setCurrentItem(index,false);
        autpPlay();
    }

    private void autpPlay(){
        new Thread(){
            @Override
            public void run() {
                super.run();
                index++;
                handler.sendEmptyMessageDelayed(1, 3000);
            }
        }.start();

    }

    private void initDots(){
        imageViewDots=new ArrayList<ImageView>(count);
        for(int i=0;i<count;i++){
            ImageView imageView=new ImageView(context);
            imageView.setImageResource(R.drawable.indicator_off);//全部设置成灰色的图片
            imageView.setPadding(5,0,5,5);
            imageViewDots.add(imageView);
            ll_dots.addView(imageView);
        }
        dotIndex=0;
        oldDotIndex=0;
        imageViewDots.get(0).setImageResource(R.drawable.indicator_on);
    }

    private void setDot(){
       imageViewDots.get(oldDotIndex).setImageResource(R.drawable.indicator_off);
       imageViewDots.get(dotIndex).setImageResource(R.drawable.indicator_on);
       oldDotIndex=dotIndex;
    }

    public View getView() {
        return view;
    }

    class AdBannerAdapter extends FragmentStatePagerAdapter{

        private List<AdBannerFragment> fragments;

        public AdBannerAdapter(FragmentManager fm,List<AdBannerFragment> fragments) {
            super(fm);
            this.fragments=fragments;
        }

        @Override
        public Fragment getItem(int i) {
            return fragments.get(i);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }
    }


}

